# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/8/1'
# __name__ = 'handles.py'
from utils.yaml_helper import YamlHelper
from utils.kubernetes_helper import KubeClient
# Create your code here.


def get_kubeinfo_from_yaml(yaml_text):
    server = None
    user = None
    yh = YamlHelper()
    data = yh.yaml_to_json(yaml_text)
    clusters = data.get('clusters', [])
    if clusters:
        server = clusters[0].get('cluster', {}).get('server')
    users = data.get('users', [])
    if users:
        user = users[0].get('name')
    return {'server': server, 'user': user}


def get_kube_deployment_info(kubeid, kubeconfig, namespace=None):
    kubecli = KubeClient(kubeid, kubeconfig)
    data = kubecli.get_namespaced_deployment_replicas_resource(namespace)
    return data